package com.etc.controller;

import com.etc.dto.Result;
import com.etc.entity.Patient;
import com.etc.service.PatientService;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@Controller
    @RequestMapping("/patient")
public class   PatientController {
    private static final Logger logger = LoggerFactory.getLogger(PatientController.class);

    @Autowired
    private PatientService patientService;

    @GetMapping("/existOfPhone")
    public @ResponseBody Result existOfPhone(@RequestParam String phone){
        Result result = new Result();
        boolean flag = patientService.existOfPhone(phone);

        return result.ok(flag);
    }

    @PostMapping("/register")
    public @ResponseBody Result register(@RequestParam String patientName,@RequestParam String phone,@RequestParam String password,@RequestParam Integer age,@RequestParam String gender){
        Result result = new Result();
        boolean flag = patientService.register(patientName, age, phone, password, gender, null);
        if(flag){
            result.ok("注册成功！");
        }else{
            result.fail(0,"注册失败！");
        }

        return result;
    }

    @PostMapping("/login")
    public @ResponseBody Result login(@RequestParam String phone, @RequestParam String password, HttpSession session){
        Result result = new Result();
        Patient patient = patientService.login(phone, password);
        if(patient!=null){
            result.ok(patient);
            session.setAttribute("user",patient.getPatientName());
        }else {
            result.fail(0,"登录失败！");
        }

        return result;
    }

    @GetMapping("/logout")
    public @ResponseBody Result logout(){
        Result result = new Result();
        patientService.logout();
        return result.ok(true);
    }

    @GetMapping("/getPatientBySession")
    public @ResponseBody Result getPatientBySession(){
        Result result = new Result();
        Patient patient = patientService.getPatientBySession();
        if(patient!=null){
            result.ok(patient);
        }else {
            result.fail(0,"无数据");
        }

        return result;
    }
    @GetMapping("/getList")
    public @ResponseBody Result getPatientList(@RequestParam Integer pageNum,@RequestParam Integer pageSize,String condition){
        Result result = new Result();
        PageInfo<Patient> pageInfo = patientService.getPatientListByPage(condition,pageNum,pageSize);
        if(!pageInfo.getList().isEmpty()){
            result.ok(pageInfo);
        }else {

            result.fail(0,"没有相关数据");
        }
        return result;
    }
    @GetMapping("/getById")
    public @ResponseBody Result getPatient(@RequestParam String patientId){
        Result result = new Result();
        Patient patient =patientService.findByid(patientId);
        if(patient!=null){
            result.ok(patient);
        }else {
            result.fail(0,"没有相关数据");
        }
        return result;
    }
    @PostMapping("/update")
    public Result updatePatient(@RequestBody Patient patient){
        Result result = new Result();
logger.debug(patient.toString());

        int n =patientService.updatePatient(patient);
        if(n>0){
            result.ok(true);
        }else {
            result.fail(0,"修改失败");
        }
        return result;
    }
}
